package com.lp.sys.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.iframework.commons.domain.enums.HttpStatusCode;
import org.iframework.commons.domain.enums.RecordStatus;
import org.iframework.commons.domain.pager.Pager;
import org.iframework.commons.util.fast.L;
import org.iframework.commons.util.fast.V;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.lp.business.domain.JsonResultModel;
import com.lp.business.model.Member;
import com.lp.sys.controller.base.BaseController;

/**
 * 会员控制器
 * 
 * @author maojunjie
 * 
 */
@Controller
@RequestMapping("/admin/member")
public class MemberController extends BaseController<Member, String> {

	/**
	 * 加载分页列表数据
	 */
	@RequestMapping(value = "list", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response, Member model) {
		ModelAndView modelAndView = new ModelAndView("/landlord/pageList");
		model.setStatus(RecordStatus.AVAILABLE);
		Pager pager = this.getPager(request);
		List<Member> models = this.baseService.findByModel(model, null, pager);
		modelAndView.addObject("models", models);
		modelAndView.addObject("page", pager);
		return modelAndView;
	}

	/**
	 * 进行保存操作
	 */
	@RequestMapping(value = "save", method = { RequestMethod.POST, RequestMethod.GET })
	public void save(HttpServletRequest request, HttpServletResponse response, Member model) {
		try {
			if (V.isNotEmpty(this.baseService.save(model))) {
				renderJson(new JsonResultModel(HttpStatusCode.SUCCESS.getDesc(), "新建保存操作成功").toSysJsonString(),
						response);
				return;
			} else {
				renderJson(new JsonResultModel(HttpStatusCode.FAILURE.getDesc(), "新建保存操作失败").toSysJsonString(),
						response);
				return;
			}
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			renderJson(new JsonResultModel(HttpStatusCode.FAILURE.getDesc(), "系统繁忙，请稍候再试").toSysJsonString(), response);
			return;
		}
	}

	/**
	 * 进行更新操作
	 */
	@RequestMapping(value = "update", method = { RequestMethod.POST, RequestMethod.GET })
	public void update(HttpServletRequest request, HttpServletResponse response, Member model) {
		// 校验是否传入id信息
		if (V.isEmpty(model.getId())) {
			renderJson(new JsonResultModel(HttpStatusCode.FAILURE.getDesc(), "ID必须填写").toSysJsonString(), response);
			return;
		}
		// 校验是否存在相应数据
		Member member = this.baseService.get(model.getId());
		if (V.isEmpty(member)) {
			renderJson(new JsonResultModel(HttpStatusCode.FAILURE.getDesc(), "找不到需要更新的数据").toSysJsonString(), response);
			return;
		}
		try {
			if (this.baseService.update(model)) {
				renderJson(new JsonResultModel(HttpStatusCode.SUCCESS.getDesc(), "更新修改操作成功").toSysJsonString(),
						response);
				return;
			} else {
				renderJson(new JsonResultModel(HttpStatusCode.FAILURE.getDesc(), "更新修改操作失败").toSysJsonString(),
						response);
				return;
			}
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			renderJson(new JsonResultModel(HttpStatusCode.FAILURE.getDesc(), "系统繁忙，请稍候再试").toSysJsonString(), response);
			return;
		}
	}

	/**
	 * 进行批量删除操作
	 */
	@RequestMapping(value = "batchDelete", method = { RequestMethod.POST, RequestMethod.GET })
	public void batchDelete(HttpServletRequest request, HttpServletResponse response) {
		// 1.执行必填校验
		String[] ids = request.getParameterValues("ids[]");
		if (V.isEmpty(ids)) {
			renderJson(new JsonResultModel(HttpStatusCode.FAILURE.getDesc(), "请选择需要删除的信息").toSysJsonString(), response);
			return;
		}
		// 2.执行删除
		try {
			Collection<Member> models = new ArrayList<Member>();
			for (String id : ids) {
				// 校验是否有此id的数据
				Member model = this.baseService.get(id);
				if (V.isEmpty(model)) {
					renderJson(
							new JsonResultModel(HttpStatusCode.FAILURE.getDesc(), "删除失败，未找到需要删除的数据").toSysJsonString(),
							response);
					return;
				}
				models.add(model);
			}
			if (this.baseService.deleteAllVirtual(models)) {
				renderJson(new JsonResultModel(HttpStatusCode.SUCCESS.getDesc(), "批量删除操作成功").toSysJsonString(),
						response);
			} else {
				renderJson(new JsonResultModel(HttpStatusCode.FAILURE.getDesc(), "批量删除操作失败").toSysJsonString(),
						response);
			}
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			renderJson(new JsonResultModel(HttpStatusCode.FAILURE.getDesc(), "系统繁忙，请稍候再试。").toSysJsonString(),
					response);
		}
	}
}
